import Vue from "vue";
import wjLoading from "./index.vue";

const LoadingConstructor = Vue.extend(wjLoading);

const instance = new LoadingConstructor({
	el: document.createElement("div"),
});

// instance.show = false
const loading = {
	show(title, bool, duration) {
		instance.show = bool;
		instance.title = title;
		instance.duration = duration;
		document.body.appendChild(instance.$el);
		if (duration) {
			setTimeout(() => {
				instance.show = false;
			}, duration);
		}
	},
	hide(bool) {
		instance.show = bool;
	},
};

export default {
	install() {
		if (!Vue.$showLoading) {
			Vue.$showLoading = loading;
		}
		Vue.mixin({
			created() {
				this.$showLoading = Vue.$showLoading;
			},
		});
	},
};
